﻿Imports System.IO
Imports System.Environment

Public Class Scavenger
    'Scavenger, a visual basic web browser
    'Copyright (C) 2011  Tetra Productions Ltd.

    'This program is free software: you can redistribute it and/or modify
    'it under the terms of the GNU General Public License as published by
    'the Free Software Foundation, either version 3 of the License, or
    '(at your option) any later version.

    'This program is distributed in the hope that it will be useful,
    'but WITHOUT ANY WARRANTY; without even the implied warranty of
    'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    'GNU General Public License for more details.

    'You should have received a copy of the GNU General Public License
    'along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Private Sub Scavenger_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddTab("www.google.com", TabControl1)
        Me.KeyPreview = True
        Me.Size = New Size(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 2)
        Dim Tab_Controller As New Tab_Controller
        Dim Refresh_Control As New Refresh_Control
        With TabControl1
            .DrawMode = TabDrawMode.OwnerDrawFixed
            .SizeMode = TabSizeMode.Fixed
            .ItemSize = New Size(200, 25)
            AddHandler .DrawItem, AddressOf Tab_Controller.Add_Visuals
            AddHandler .MouseClick, AddressOf Tab_Controller.Mouse_Click
            AddHandler .MouseMove, AddressOf Tab_Controller.Mouse_Hover
            AddHandler .MouseDown, AddressOf Tab_Controller.Mouse_Down
            AddHandler .MouseUp, AddressOf Tab_Controller.Mouse_Up
        End With
        With Me
            AddHandler .MouseMove, AddressOf Tab_Controller.Mouse_Hover
            AddHandler .MouseClick, AddressOf Tab_Controller.Mouse_Click
            AddHandler .MouseLeave, AddressOf Tab_Controller.Mouse_Leave
        End With
        With RFRSH
            AddHandler .MouseDown, AddressOf Refresh_Control.Drag_Check
            AddHandler .MouseUp, AddressOf Refresh_Control.Up
            AddHandler .MouseMove, AddressOf Refresh_Control.Drag
        End With
        Dim HistoryModule As New HistoryModule
        HistoryModule.AutoComplete_Read()
        HistoryModule.Generic_Read()
    End Sub
    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Dim GB As ScavengerBrowser = Me.TabControl1.SelectedTab.Tag
        If GB IsNot Nothing Then
            If Me.TabControl1.SelectedTab.Controls.Count > 0 Then
                If GB.Url IsNot Nothing Then
                    URL.Text = GB.Url.ToString()
                    Me.Text = GB.DocumentTitle
                End If
            End If
        End If
        If TabControl1.SelectedTab.Text = "" Then
            Me.Text = "Scavenger"
        End If
        If GB.CanGoBack Then
            BCK.Enabled = True
        Else
            BCK.Enabled = False
        End If
        If GB.CanGoForward Then
            FWD.Enabled = True
        Else
            FWD.Enabled = False
        End If
    End Sub
    Private Sub Form_KeyDown(ByVal sender As System.Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
        Dim GB As New ScavengerBrowser
        If Me.ActiveControl.Name = "ScavengerBrowser" Then
            If Keys.Control And e.KeyCode = Keys.O Then
                OpenFileDialog1.ShowDialog()
            End If
        End If
    End Sub
    Private Sub GO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GO.Click
        If URL.Text <> "" Then
            Dim GB As ScavengerBrowser = Me.TabControl1.SelectedTab.Tag
            GB.Navigate(URL.Text)
            Me.AcceptButton = Nothing
        End If
    End Sub
    Public Sub AddTab(ByRef WWW As String, ByRef TabControl As TabControl)
        Dim scavenge As New ScavengerBrowser
        Dim NewTab As New TabPage
        scavenge.Tag = NewTab
        NewTab.Tag = scavenge
        TabControl.TabPages.Add(NewTab)
        NewTab.Controls.Add(scavenge)
        scavenge.Dock = DockStyle.Fill
        scavenge.Navigate(WWW)
        NewTab.Text = WWW
        TabControl1.SelectTab(TabControl1.TabPages.Count - 1)
        URL.Text = WWW
    End Sub
    Private Sub RFRSH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RFRSH.Click
        Dim GB As ScavengerBrowser = Me.TabControl1.SelectedTab.Tag
        GB.Refresh()
    End Sub

    Private Sub BCK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BCK.Click
        Dim GB As ScavengerBrowser = Me.TabControl1.SelectedTab.Tag
        GB.GoBack()
    End Sub

    Private Sub FWD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FWD.Click
        Dim GB As ScavengerBrowser = Me.TabControl1.SelectedTab.Tag
        GB.GoForward()
    End Sub
    Private Sub SCHRG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SRCHG.Click
        Dim GB As ScavengerBrowser = Me.TabControl1.SelectedTab.Tag
        If SRCH.Text <> "" Then
            GB.Navigate("http://www.google.com/search?q=" & SRCH.Text.Replace(" ", "+"))
        Else
            GB.Navigate("http://www.google.com")
        End If
    End Sub
    Private Sub SRCH_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SRCH.Enter
        Me.AcceptButton = SRCHG
    End Sub

    Private Sub URL_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles URL.Enter
        Me.AcceptButton = GO
    End Sub
    Private Sub Scavenger_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.TextChanged
        If Me.Text <> "" Then
        Else
            Me.Text = "Scavenger"
        End If
    End Sub

    Private Sub RFRSH_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RFRSH.MouseHover
        RFRSH.BackgroundImage = My.Resources.Btn_Base_Hover
    End Sub

    Private Sub RFRSH_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RFRSH.MouseLeave
        RFRSH.BackgroundImage = My.Resources.Btn_Base
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim temp As New Dev_Debug
        temp.Show()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewTabToolStripMenuItem.Click
        AddTab("about:Home", TabControl1)
    End Sub

    Private Sub NewWindowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewWindowToolStripMenuItem.Click
        Process.Start(Application.StartupPath + "\" + Me.Name() + ".exe")
    End Sub

    Private Sub OpenFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
    End Sub
End Class